/**
 * Created by lwy on 2017/5/21.
 */

var isRemember =true;
var remember;
$(function () {
    var name = localStorage.getItem('crmphone');
    var pass = localStorage.getItem('crmpwd');
    if (pass != null) {
        remember = true;
        $('#phone').val(name);
        $('#password').val(pass);
    } else {
        remember = false;
    }

});
$('#phone').keydown(function () {
    remember = false;
});
$('#password').keydown(function () {
    remember = false;
});

function check(num) {
    $('#login_img' + num).hide();
    $('#login_img' + (3 - num)).show();
    if (num == 1) {
        isRemember = false;
    } else {
        isRemember = true;
    }
}

//回车登录
function keyLogin(){
    if (event.keyCode==13){
        if($('#login-button1').is(':hidden')){
            document.getElementById("login-button2").click(); //调用登录按钮的登录事件
        }else if($('#login-button2').is(':hidden')){
            document.getElementById("login-button1").click(); //调用登录按钮的登录事件
        }
    }
}

//账号密码登录
function loginButton1(){
    var userName = $('#phone').val();
    var passWord = $('#password').val();
    $.ajax({
        url:'/crm/shiroLongC/userLoginKey.json',
        dataType:'json',
        data:{
            userName:userName,
        },
        success:function(data){
            if(data.code!=1){
                alert(data.msg);
                return;
            }
            if(data.code==1){
                    passWord= calcMD5(passWord + data.datas.userKey, 32);
                $.ajax({
                    url:'/crm/shiroLongC/crmLogin.json',
                    dataType:'json',
                    type:'post',
                    data:{
                        userName:userName,
                        passWord: remember ? localStorage.getItem('crmpwd') :passWord,
                    },
                    success:function(data){
                        if(data.code!=1){
                            alert(data.msg);
                            return;
                        }
                        if(data.code==1){
                            localStorage.setItem('crmtoken',data.datas.token);
                            localStorage.setItem('crmphone',userName);
                            localStorage.setItem('crmpwd',passWord);
                            localStorage.setItem('crmrole',data.datas.role);
                            localStorage.setItem('roleValue',data.datas.roleValue);
                            window.location.href="/crm/index.jsp";
                        }
                    }
                })
            }
        }
    })
}


